package com.wowangz.cms.systemmanage.channel.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wowangz.cms.common.utils.UUIDGenerator;
import com.wowangz.cms.syscore.exception.ServiceException;
import com.wowangz.cms.syscore.service.BaseService;
import com.wowangz.cms.systemmanage.channel.dto.ChannelInfo;
import com.wowangz.cms.systemmanage.channel.dto.GoodsInfo;
import com.wowangz.cms.systemmanage.channel.entity.Channel;
import com.wowangz.cms.systemmanage.channel.entity.ChannelPrice;

public class ChannelService extends BaseService {

	private Logger logger = LoggerFactory.getLogger(ChannelService.class);
	
	/**
	 * 商家优惠分页查询总条数
	 * 
	 */
	public long findResultCount(HashMap<String, Object> map) throws ServiceException {
		try {
			Long countNum = this.getSqlSession().selectOne("Channel.queryChannelInfoCount", map);
			return countNum;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}			
	}

	/**
	 * 商家优惠查询记录集合
	 * 
	 */
	public List<ChannelInfo> findResultList(HashMap<String, Object> map) throws ServiceException {
		try {
			List<ChannelInfo> list = this.getSqlSession().selectList("Channel.queryChannelInfoPage", map);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}		
	}
	
	/**
	 * 验证商家优惠信息
	 */
	public boolean validationChannel(HashMap<String, Object> map) throws ServiceException {
		try {
			Long countNum = this.getSqlSession().selectOne("Channel.validationChannel", map);
			if(countNum != 0){
				return false;
			}else{
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}		
	}
	
	/**
	 * 新增商家信息
	 *
	 */
	public void addChannel(Channel channel) throws ServiceException {
		try {
			this.getSqlSession().insert("Channel.insertChannel", channel);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}		
	}
	
	/**
	 * 新增商家优惠
	 *
	 */
	public void addChannelPrice(ChannelPrice channelPrice) throws ServiceException {
		try {
			this.getSqlSession().insert("Channel.insertChannelPrice", channelPrice);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}		
	}
	/**
	 * 保存优惠商家优惠信息
	 * @Title: addChannelInfo 
	 * @Description: 
	 * @return void
	 * @author Green.Long
	 * @date 2016年8月18日 下午4:48:34
	 */
	public void addChannelInfo(Channel channel,String[] ids,String[] preferentialPrices,String userId){
		ChannelPrice channelPrice = new ChannelPrice();
		if(ids == null || ids.length == 0){		
			return;
		}
		channel.setId(UUIDGenerator.getUUID());
		channel.setDataStatus("InUse");
		channel.setCreateTime(new Date());
		addChannel(channel);//新增商家信息
		for(int i = 0; i < ids.length; i++){
			channelPrice.setId(UUIDGenerator.getUUID());
			channelPrice.setChannelId(channel.getId());
			channelPrice.setGoodsId(Long.valueOf(ids[i]));
			channelPrice.setMoney(Double.valueOf(preferentialPrices[i]));
			channelPrice.setCreateTime(new Date());
			channelPrice.setCreateUserId(userId);
			channelPrice.setModifyTime(new Date());			
			addChannelPrice(channelPrice);//新增商家优惠
		}
	}
	
	/**
	 * 商品信息分页查询总条数
	 * 
	 */
	public long findGoodsResultCount(HashMap<String, Object> map) throws ServiceException {
		try {
			Long countNum = this.getSqlSession().selectOne("Channel.queryGoodsInfoCount", map);
			return countNum;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}			
	}

	/**
	 * 商品信息查询记录集合
	 * 
	 */
	public List<GoodsInfo> findGoodsResultList(HashMap<String, Object> map) throws ServiceException {
		try {
			List<GoodsInfo> list = this.getSqlSession().selectList("Channel.queryGoodsInfoPage", map);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}		
	}
	
	/**
	 * 根据ID查询商家优惠信息
	 *
	 */
	public ChannelInfo findChannelInfoById(String id) throws ServiceException {
		try {
			ChannelInfo channelInfo = this.getSqlSession().selectOne("Channel.findChannelInfoById", id);
			return channelInfo;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}			
	}
	
	/**
	 * 修改商家优惠
	 *
	 */
	public void updateChannelPrice(ChannelPrice channelPrice) throws ServiceException {
		try {
			this.getSqlSession().update("Channel.updateChannelPrice", channelPrice);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}	
	}
	
	/**
	 * 修改商家信息
	 *
	 */
	public void updateChannel(Channel channel) throws ServiceException {
		try {
			this.getSqlSession().update("Channel.updateChannel", channel);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}	
	}
	
}
